<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文本作业提交调试页面</title>
    <link rel="stylesheet" href="/static/css/layui.css">
</head>
<body>
    <div style="padding: 20px;">
        <h2>文本作业提交调试</h2>
        
        <div class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">作业ID</label>
                <div class="layui-input-block">
                    <input type="text" id="homework_id" value="3333" class="layui-input" style="width: 200px;">
                </div>
            </div>
            
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">文本内容</label>
                <div class="layui-input-block">
                    <textarea id="text_content" class="layui-textarea" style="width: 600px; height: 200px;">这是一个测试的文本作业内容，用于调试提交功能。</textarea>
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" id="test_submit" class="layui-btn">测试提交</button>
                    <button type="button" id="check_session" class="layui-btn layui-btn-normal">检查Session</button>
                </div>
            </div>
        </div>
        
        <div style="margin-top: 20px;">
            <h3>调试信息</h3>
            <div id="debug_info" style="background: #f5f5f5; padding: 10px; border-radius: 4px; font-family: monospace; white-space: pre-wrap; min-height: 200px;"></div>
        </div>
    </div>

    <script src="/static/layui.js"></script>
    <script>
        layui.use(['layer', 'jquery'], function() {
            var layer = layui.layer;
            var $ = layui.jquery;
            
            function log(message) {
                var debugInfo = document.getElementById('debug_info');
                var timestamp = new Date().toLocaleTimeString();
                debugInfo.textContent += '[' + timestamp + '] ' + message + '\n';
                debugInfo.scrollTop = debugInfo.scrollHeight;
                console.log(message);
            }
            
            // 检查Session
            $('#check_session').on('click', function() {
                log('检查Session状态...');
                
                $.ajax({
                    type: 'GET',
                    url: '/login/check_session',
                    dataType: 'json',
                    success: function(result) {
                        log('Session检查结果: ' + JSON.stringify(result, null, 2));
                    },
                    error: function(xhr, status, error) {
                        log('Session检查失败: ' + xhr.status + ' ' + error);
                        log('响应内容: ' + xhr.responseText);
                    }
                });
            });
            
            // 测试提交
            $('#test_submit').on('click', function() {
                var homeworkId = $('#homework_id').val();
                var textContent = $('#text_content').val();
                
                log('开始测试提交...');
                log('作业ID: ' + homeworkId);
                log('文本长度: ' + textContent.length);
                
                var submitData = {
                    ID: parseInt(homeworkId),
                    text_content: textContent,
                    submission_type: 'text'
                };
                
                log('提交数据: ' + JSON.stringify(submitData, null, 2));
                
                var loadIndex = layer.load(2, { shade: [0.1, '#fff'] });
                
                $.ajax({
                    type: 'POST',
                    url: '/homework/submit',
                    contentType: 'application/json;charset=utf-8',
                    dataType: 'json',
                    data: JSON.stringify(submitData),
                    success: function(result) {
                        layer.close(loadIndex);
                        log('提交成功! 响应: ' + JSON.stringify(result, null, 2));
                        
                        try {
                            if (typeof result === 'string') {
                                result = JSON.parse(result);
                            }
                            
                            if (result.code === 0) {
                                layer.msg('提交成功！', {icon: 1});
                            } else {
                                layer.msg('提交失败: ' + result.msg, {icon: 2});
                            }
                        } catch (e) {
                            log('解析响应失败: ' + e.message);
                        }
                    },
                    error: function(xhr, status, error) {
                        layer.close(loadIndex);
                        log('提交失败!');
                        log('状态码: ' + xhr.status);
                        log('状态: ' + status);
                        log('错误: ' + error);
                        log('响应头: ' + JSON.stringify(xhr.getAllResponseHeaders()));
                        log('响应内容: ' + xhr.responseText);
                        
                        var errorMsg = '提交失败';
                        if (xhr.status === 401) {
                            errorMsg = '请先登录';
                        } else if (xhr.status === 403) {
                            errorMsg = '权限不足';
                        } else if (xhr.status === 404) {
                            errorMsg = '接口不存在';
                        } else if (xhr.status === 500) {
                            errorMsg = '服务器错误';
                        }
                        
                        layer.msg(errorMsg, {icon: 2});
                    },
                    timeout: 30000
                });
            });
            
            // 页面加载完成后自动检查Session
            setTimeout(function() {
                $('#check_session').click();
            }, 500);
        });
    </script>
</body>
</html>
